/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import { Menu } from "antd";
import * as React from "react";
import { Link } from "react-router-dom";
import { isArray } from "util";
import { homePage } from "../../config/base";
import menuData, { IMenu } from "../../common/menu";
import { getAuthority } from "../../utils/authority";
import { defaultAuthority } from "../../config/base";
import Cookies from "js-cookie";

const SubMenu = Menu.SubMenu;

// 类型保护 页面如果没有设置权限，则使用默认权限，默认权限为null的话，则默认不设权限
function f(sn: string | string[] | undefined): string | string[] {
  return sn || (defaultAuthority === null ? "noAuthority" : defaultAuthority);
}

function typeOfChange(value: any) {
  if (typeof value === "string") {
    return JSON.parse(value);
  }
}

const roleMenu: any = Cookies.get("user_role");
const user: any = Cookies.get("user");
function getMenuData(menu: IMenu[], parentPath?: string) {
  let userAuthority: string[] | string = getAuthority();
  return menu.map((item: IMenu) => {
    if (
      typeOfChange(roleMenu).findIndex((v: any) => v.path === item.path) ===
        -1 &&
      typeOfChange(user).supervise !== 1
    ) {
      return;
    }
    let authority: string | string[] = f(item.authority);
    let isShow = false;

    if (authority === "noAuthority") {
      isShow = true;
    } else {
      if (!isArray(userAuthority)) {
        if (userAuthority.indexOf(",") === -1) {
          userAuthority = [userAuthority];
        } else {
          userAuthority = userAuthority.split(",");
        }
      }
      if (!isArray(authority)) {
        if (authority.indexOf(",") === -1) {
          authority = [authority];
        } else {
          authority = authority.split(",");
        }
      }
      if (isArray(userAuthority) && isArray(authority)) {
        userAuthority.forEach(userele => {
          for (const ele of authority) {
            if (userele === ele) {
              isShow = true;
            }
          }
        });
      }
    }

    if (!isShow) {
      return null;
    }

    if (item.children) {
      return (
        <SubMenu
          key={item.name}
          title={
            <span style={{ display: "flex", alignItems: "center" }}>
              {item.icon ? (
                <img
                  style={{ width: "14px", height: "13px", marginRight: "10px" }}
                  src={item.icon}
                />
              ) : null}
              {item.name}
            </span>
          }
        >
          {getMenuData(item.children, item.path)}
        </SubMenu>
      );
    }
    return (
      <Menu.Item key={parentPath ? parentPath + item.path : item.path}>
        <Link to={parentPath ? parentPath + item.path : item.path}>
          <span style={{ display: "flex", alignItems: "center" }}>
            {item.icon ? (
              <img
                style={{ width: "14px", height: "13px", marginRight: "10px" }}
                src={item.icon}
              />
            ) : null}
            {item.name}
          </span>
        </Link>
      </Menu.Item>
    );
  });
}

interface IProps extends React.HTMLAttributes<HTMLDivElement> {}

export default class MenuLink extends React.Component<IProps> {
  public getSelectdkeys = () => {
    if (location.search.indexOf("?page") === -1) {
      return location.pathname === "/" ? homePage : location.pathname;
    }
    return location.pathname === "/"
      ? homePage
      : location.pathname + location.search.split("&")[0];
  };
  public render() {
    return (
      <Menu
        theme="dark"
        selectedKeys={[this.getSelectdkeys()]}
        mode="inline"
        inlineCollapsed={false}
        style={this.props.style}
      >
        {getMenuData(menuData)}
      </Menu>
    );
  }
}
